home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-04-27 | 5.8 KB | 150 lines |
- import java.lang.*;
- import java.io.*;
- import java.util.*;
- // This is the 'main' Adventure unit - the object that contains the map of Rooms
- // and their contents
-
- public class Adventure extends java.lang.Object
- implements java.io.Serializable,
- AdventureConstants
- {
-
- private Vector map = new Vector(); // the map - a Vector of Rooms
- private AdvUtils Utils = new AdvUtils(); // My handy toolkit
- private Person player; // the player - which provides our 'first person perspective'
-
- /******** THE MAP *******
- For reference, here is a simple 'picture' of the map
- showing how the rooms are connected:
-
- room0 -- room1
- |
- room2 -- room3
- | |
- room4 -- room5
-
- *************************/
-
- Adventure() {
- // --- construct a new adventure ---
- // Add Rooms to the map
- // Room( name, description, N, S, W, E )
- map.addElement(new Room("room0, West of House", "An open field west of a white house", NOEXIT, 2, NOEXIT, 1));
- map.addElement(new Room("room1, White House", "A white house in the Mexican style", NOEXIT, NOEXIT, 0, NOEXIT));
- map.addElement(new Room("room2, Gold room", "A golden room hung with rich tapestries", 0, 4, NOEXIT, 3));
- map.addElement(new Room("room3, Troll room", "A dank, evil-smelling dungeon", NOEXIT, 5, 2, NOEXIT));
- map.addElement(new Room("room4, Dark cave", "It's too dark to see anything!", 2, NOEXIT, NOEXIT, 5));
- map.addElement(new Room("room5, Orchard", "A sweetly-scented orchard", 3, NOEXIT, 4, NOEXIT));
- // --- Add things to selected Rooms
- // room0
- ((Room)map.elementAt(0)).addthing(new Thing("Sword", "An Elvish sword of great antiquity"));
- ((Room)map.elementAt(0)).addthing(new Thing("Bubblegum", "A pink, sticky, dried-up blob"));
- ((Room)map.elementAt(0)).addthing(new Thing("Dagger", "A blood-stained dagger"));
- // room3
- ((Room)map.elementAt(3)).addthing(new Thing("Chewing gum", "A grey, sticky, dried-up blob"));
- // room5
- ((Room)map.elementAt(5)).addthing(new Thing("Fluff", "Some pocket fluff"));
- ((Room)map.elementAt(5)).addthing(new Thing("Coin", "A silver sixpenny bit"));
- ((Room)map.elementAt(5)).addthing(new Thing("Pot of noodles", "A vaguely inedible-looking delicacy"));
- // create player and place in Room 0 (i.e. the Room at 0 index of map Vector)
- player = new Person((Room)map.elementAt(0));
- }
-
- // access methods
- // map
- Vector getmap() {
- return map;
- }
-
- void setmap(Vector aMap) {
- map = aMap;
- }
-
- // player
- Person getplayer() {
- return player;
- }
-
- void setplayer(Person aPlayer) {
- player = aPlayer;
- }
-
- // move a Person (typically the player) to a Room
- void movePersonTo( Person p, Room aRoom ) {
- p.setroom( aRoom );
- }
-
- // move a Person 'p' in direction 'dir'
- int moveTo( Person p, int dir ) {
- // return: Constant representing the room number moved to
- // or NOEXIT
- //
- // try to move any Person (typically but not necessarily player)
- // in direction indicated by dir
- Room r = p.getroom();
- int exit;
-
- switch( dir ) {
- case NORTH : exit = r.getn();
- break;
- case SOUTH : exit = r.gets();
- break;
- case EAST : exit = r.gete();
- break;
- case WEST : exit = r.getw();
- break;
- default: exit = NOEXIT; // noexit - stay in same room
- break;
- }
- if (exit > NOEXIT)
- movePersonTo( p,(Room) map.elementAt(exit) );
- return exit;
- }
-
- String movePlayerTo(int dir) {
- // !!! Now returns a String (a message to be displayed) rather than an integer
- // return: Constant representing the direction moved
- // or NOEXIT (see moveTo())
- //
- if (moveTo(player, dir ) == NOEXIT)
- return "No Exit!";
- else return "";
- }
-
-
-
- String takeOb( String obname ) {
- // If object specified by obname is in current room, Transfer it from
- // Room's object Vector to Player's object Vector
- Vector inventory = player.getthings();
- Vector thingshere = player.getroom().getthings();
- int obindex = Utils.ObNameAtIndex( obname, thingshere );
- if (obname.equals(""))
- return ("I don't know which thing you want to take!");
- else if (obindex == -1)
- return "There is no " + obname + " here!";
- else {
- Utils.TransferOb(thingshere.elementAt(obindex), thingshere, inventory );
- return obname + " taken!";
- }
- }
-
-
- String dropOb( String obname ) {
- // If object specified by obname is in inventory, Transfer it from
- // Player's object Vector to Room's object Vector
- Vector inventory = player.getthings();
- Vector thingshere = player.getroom().getthings();
- int obindex = Utils.ObNameAtIndex( obname, inventory );
- if (obname.equals(""))
- return ("I don't know which thing you want to drop!");
- if (obindex == -1)
- return "You haven't got the " + obname + "!";
- else {
- Utils.TransferOb(inventory.elementAt(obindex), inventory, thingshere );
- return obname + " dropped!";
- }
- }
-
- }
-